package org.example.redis.demo;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ListRedisDemo {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //存放
    public void a(){
        //对集合类型的容器进行操作
        ListOperations<String, String> ops = stringRedisTemplate.opsForList();
        //dd cc bb aa
        ops.leftPush("A", "aa");
        ops.leftPush("A", "bb");
        ops.leftPush("A", "cc");
        ops.leftPush("A", "dd");
        //gg ff ee dd cc bb aa
        ops.leftPushAll("A", "ee","ff","gg");
        //gg ff ee dd cc bb aa 11 22 33 44
        ops.rightPush("A","11");
        ops.rightPush("A","22");
        ops.rightPush("A","33");
        ops.rightPush("A","44");
        //gg ff ee dd cc bb aa 11 22 33 44 55 66 77
        ops.rightPushAll("A","55","66","77");
    }
    //获取
    public void b(){
        ListOperations<String, String> ops = stringRedisTemplate.opsForList();
        String a5 = ops.index("A", 5);
        String a11 = ops.index("A", 11);
        log.info("a5:{},a11:{}", a5, a11);
        //获取全部
        List<String> allList = ops.range("A", 0, -1);
        List<String> list1 = ops.range("A", 1, 6);
        log.info("所以内容:{}", allList);
        log.info("部分内容:{}", list1);
    }
  //其他
    public void c(){
        ListOperations<String, String> ops = stringRedisTemplate.opsForList();
        Long size = ops.size("A");
        ops.set("A",2,"kk");
        ops.remove("A",4,"aa");
    }
}
